package org.vectrics.recruiter.struts;

import javax.servlet.jsp.JspException;

import org.apache.log4j.Logger;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.location.Location;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.JobFunction;
import org.vectrics.recruiter.user.CandidateInfo;
import org.vectrics.recruiter.user.JobAgent;
import org.vectrics.struts.VectricsActionData;
import org.vectrics.struts.VectricsStrutsAction;
import org.vectrics.user.Person;

public class SaveJobAgentAction extends VectricsStrutsAction {
    private transient static Logger log = Logger.getLogger(SaveJobAgentAction.class);
    //private boolean newJob = false;

	
	public String doExecute(VectricsActionData actionData)	throws JspException {
	   log.debug("execute() called");
	   try {
	       if (actionData.wasButtonPressed("cancel") == false) {
				JobAgentForm jobAgentForm = (JobAgentForm)actionData.getForm();
				
				//String personId = jobAgentForm.getPersonId();
				String agentId = jobAgentForm.getJobAgentId();
				JobAgent jobAgent = null;
				
				Person person = CoreServiceLocator.getPersonService().findPersonById(new Long(jobAgentForm.getPersonId()));
				CandidateInfo candidateInfo = RecruitServiceLocator.getRecruitUserService().createCandidateInfoIfNotExist(person);
				RecruitServiceLocator.getRecruitUserService().updateCandidateInfo(candidateInfo);
				
				if ((agentId != null) && (agentId.trim().length() > 0)) {
					jobAgent = RecruitServiceLocator.getRecruitUserService().findJobAgent(new Long(agentId));
					if (jobAgent == null) {
						throw new JspException("Could not find agent, id = " + agentId);
					}
				} else {
					jobAgent = new JobAgent();
					jobAgent.setCandidateInfo(candidateInfo);
				}
				
				jobAgent.setActive(jobAgentForm.getActive().toLowerCase().startsWith("tr"));
				jobAgent.setSearchPhrase(jobAgentForm.getSearchPhrase());
				String daysBackParam = jobAgentForm.getDaysBack();
				int daysBack = 3;
				try {
				    daysBack = Integer.parseInt(daysBackParam);
				} catch (Exception ex) {
				}
				
				jobAgent.setDaysBack(new Integer(daysBack));
				jobAgent.setName(jobAgentForm.getName());
				
				if ((jobAgent.getName() == null) || (jobAgent.getName().trim().length() <= 0)) {
					jobAgent.setName("Job Agent");
				}
				RecruitServiceLocator.getRecruitUserService().updateJobAgent(jobAgent);
		
				if (jobAgent.getJobFunctions() != null)
					jobAgent.getJobFunctions().clear();
				
				if (jobAgent.getLocations() != null)
					jobAgent.getLocations().clear();
				
				saveFunction(jobAgent, jobAgentForm.getFunctionCode1());
				saveFunction(jobAgent, jobAgentForm.getFunctionCode2());
				saveFunction(jobAgent, jobAgentForm.getFunctionCode3());
				
				saveLocation(jobAgent, jobAgentForm.getLocationId1());
				saveLocation(jobAgent, jobAgentForm.getLocationId2());
				saveLocation(jobAgent, jobAgentForm.getLocationId3());
				
				jobAgent.setSendFailureCount(new Integer(0));
				RecruitServiceLocator.getRecruitUserService().updateJobAgent(jobAgent);
		    	ThreadProperties.forcedCommitAndNewTransaction();
	       }
	   }
	   catch (Exception ex) {
	   	log.error("Exception", ex);
	   	throw new JspException("Exception in ShowJobAgentAction", ex);
	   }
	   log.info("execute() - finished.");
	   
	   return("success");
	}
	
	
	private void saveFunction(JobAgent jobAgent, String functionCode) {
		if ((functionCode != null) && (functionCode.trim().length() > 0)) {
			JobFunction jobFunction = RecruitServiceLocator.getJobProvider().findFunction(functionCode);
			if (jobFunction != null) {
				jobAgent.addJobFunction(jobFunction);
			}
		}
	}
	
	private void saveLocation(JobAgent jobAgent, String locationId) {
		if ((locationId != null) && (locationId.trim().length() > 0)) {
			Location location = CoreServiceLocator.getLocationService().findLocation(new Long(locationId));
			jobAgent.addLocation(location);
		}
	}

	
    
}
